package com.fast.alden.portal.security.service.impl;

import com.fast.alden.data.model.PortalUser;
import com.fast.alden.portal.security.model.PortalUserDetailsImpl;
import com.fast.alden.portal.service.PortalUserPortalService;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class PortalUserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private PortalUserPortalService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        PortalUser user = userService.findByAccount(username);
        if (user == null) {
            throw new UsernameNotFoundException("username " + username + " is not found");
        }

        PortalUserDetailsImpl userDetails = new PortalUserDetailsImpl();
        userDetails.setUser(user);

        return userDetails;
    }
}
